import subprocess
import os


# video_output = 'D:\wts' #其他随意位置也可以生成


# video_output = 'E:\news_server\chuanmei_news-kong_dev (1)\chuanmei_news-kong_dev (1)\news_server\media'  # 路径有中文括号和空格，可能有影响


def merged_data_process(file_name, news_id):  # 短视频算法整合
    cmd_data_process = "python E:\\nba-short-video-unsupervised-reserved\\data_processing.py -op all -f " + file_name
    ret = subprocess.Popen(cmd_data_process, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                           encoding='utf-8')
    return ret


def merged_video_test(file_name, news_id):  # 短视频算法整合
    print('Start Visualization for {}'.format(file_name))
    proportion = 0.015
    if '-1' in file_name:
        proportion = 0.055
    cmd_video_test = "python E:\\nba-short-video-unsupervised-reserved\script\\video-summary\\main.py -d E:\\nba-short-video-unsupervised-reserved\\data\\h5s\\nba-dataset-inception_v1.h5 --save-dir E:\\nba-short-video-unsupervised-reserved\\result -s E:\\nba-short-video-unsupervised-reserved\\nba-split.json --split-id 0 -m summe --gpu 0 --resume E:\\nba-short-video-unsupervised-reserved\\result\\model_epoch3.pth.tar --verbose --evaluate --save-results --proportion %f" % (
        proportion) + " --test-specific --file " + file_name
    ret = subprocess.Popen(cmd_video_test, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                           encoding='utf-8')
    return ret


def merged_video_visualise(file_name, news_id):  # 短视频算法整合
    root_video_output = 'E:\\proj\\chuanmei_news-kong_dev\\chuanmei_news-kong_dev\\news_server\\media\\videos'  # 视频生成位置，可以生成
    video_output = os.path.join(root_video_output, news_id)
    if not os.path.exists(video_output):
        os.makedirs(video_output)
    cmd_video_visualise = "python E:\\nba-short-video-unsupervised-reserved\script\\video-summary\\summary2video.py -d E:\\nba-short-video-unsupervised-reserved\\data\\frames --save-dir " + video_output + " -i 0 --fps 25 --width 640 --height 480 -p E:\\nba-short-video-unsupervised-reserved\\result\\result.h5 --save-name " + file_name + ".mp4"
    ret = subprocess.Popen(cmd_video_visualise, shell=True, stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           encoding='utf-8')
    print('End Visualization for {}'.format(file_name))
    return ret


def message_out(ret):  # 打印
    while ret.poll() is None:
        line = ret.stdout.readline()
        line = line.strip()
        if line and ('End' in line or 'Start' in line):
            print(line)
    if ret.returncode == 0:
        pass
    else:
        print("Fail")


def merged_writebot(file_name):  # 文本生成算法整合
    writebot_script_path = 'D:\\WriteBot\\untitled\\main.py'
    print('Start Write_bot for {}'.format(file_name))
    cmd_line = "python " + writebot_script_path + ' --news_id ' + file_name
    ret = subprocess.Popen(cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                           encoding='gb18030')  # 调用算法
    print('End Write_bot for {}'.format(file_name))
    # return ret.stdout
    return ret.stdout.read()

# if __name__ == '__main__':
#     video_file = '155832-4'
#     text_file = '155832'
#     ret_data_process = merged_data_process(video_file)
#     message_out(ret_data_process)
#     ret_video_test = merged_video_test(video_file)
#     message_out(ret_video_test)
#     ret_video_visualise = merged_video_visualise(video_file)
#     message_out(ret_video_visualise)
#     write = merged_writebot(text_file)
#     print(type(write))
#     print(str(write))
